package com.example.score_reception.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.score_reception.entity.Role;
import com.example.score_reception.entity.Student;
import com.example.score_reception.entity.StudentRole;
import com.example.score_reception.mapper.StudentMapper;
import com.example.score_reception.service.RoleService;
import com.example.score_reception.service.StudentRoleService;
import com.example.score_reception.service.StudentService;
import com.example.score_reception.util.ContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService, UserDetailsService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private StudentRoleService studentRoleService;
    @Override
    public UserDetails loadUserByUsername(String s) throws AuthenticationException {
        HttpServletRequest request = ContextHolderUtils.getRequest();
        String password = request.getParameter("stuPassword");
        BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        Map<String,Object> map = new HashMap<>();
        map.put("stu_number",s);
        List<Student> studentList = studentMapper.selectByMap(map);
        /**
         * 因为不想用security本身的msg消息，就提前做了一下想要的msg
         */
        if(s == null || password == null) {
            throw new BadCredentialsException("用户名或密码不能为空");
        }
        if(studentList.size() == 0 || studentList.get(0) == null) {
            throw new BadCredentialsException("帐号不存在，请重新输入");
        }
        if(!bcryptPasswordEncoder.matches(password, studentList.get(0).getPassword())) {
            throw new BadCredentialsException("密码错误");
        }
        if(studentList.get(0).getStuStatus() == 0) {
            throw new BadCredentialsException("账号已被封禁");
        }
        List<StudentRole> studentRoleList = studentRoleService.list(new LambdaQueryWrapper<StudentRole>().eq(StudentRole::getStuId,studentList.get(0).getStuId()));
        List<Long> roleIds = new ArrayList<>();
        for (StudentRole userRole : studentRoleList) {
            roleIds.add(userRole.getRoleId());
        }
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>().in(Role::getRoleId,roleIds));
        studentList.get(0).setRoles(roles);
        return studentList.get(0);
    }

}

